﻿<%@ WebHandler Language="C#" Class="UserHandler" %>

using System;
using System.Web;
using System.Collections.Generic;
using EHSubmit.Util;
using EHSubmit.Logic;
using EHSubmit.Model;

//////url:'UserHandler.ashx?op=Add&params=json';
/// <summary>
/// Ajax用户处理类
/// </summary>
public class UserHandler : BaseAjaxHandler {
    
    protected override void Mapping()
    {
        
        //添加业务逻辑方法

        OprationMap["Update"] = "Update";  //修给用户信息
        OprationMap["AlterPwd"] = "AlterPwd";  // 修改密码
        OprationMap["GetUser"] = "GetUser";    // 获取用户
        OprationMap["GetUserById"] = "GetUserById";    // 获取指定用户
    }

    public void AlterPwd()
    {
        try
        {
            User user = new User();
            UserManager Manager = new UserManager();
            user = Manager.GetUser(CurrentUser.ID);
            if (WebHelper.GetMD5(Context.Request["OldPwd"]) == user.Pwd)
            {
                user.Pwd = WebHelper.GetMD5(Context.Request["NewPwd"]);
                Manager.UpdateUser(user);
                IsSuccess = true;
                ResponseText = "{success:true,msg:'操作成功'}";
            }
            else
            {
                //ResponseText = "{success:false,msg:'输入密码错误'}";
                Message = "密码错误";
            }
            
        }
        catch (Exception e)
        {
            Message = e.Message;
        }
    }



    public override void Update()
    {

            try
            {
                if (Validate(ERole.Student))
                {
                    User user = CurrentUser;
                    UserManager Manager = new UserManager();
                    user = Manager.GetUser(CurrentUser.ID);
                    user.Name = Context.Request["Name"];
                    user.Sex = Context.Request["Sex"];
                    user.Phone = Context.Request["Phone"];
                    user.Birthday = Convert.ToDateTime(Context.Request["Birthday"]); //日期转化

                    user.Class = Context.Request["Class"];
                    user.Department = Context.Request["Department"];
                    user.Email = Context.Request["Email"];
                    user.Institute = Context.Request["Institute"];

                    Manager.UpdateUser(user);
                    IsSuccess = true;

                    CurrentUser = user;

                    ResponseText = "{success:true,msg:'操作成功'}";
                }
                else if (Validate(ERole.Teacher))
                {
                    User user = CurrentUser;
                    UserManager Manager = new UserManager();
                    user = Manager.GetUser(CurrentUser.ID);
                    user.Name = Context.Request["Name"];
                    user.Sex = Context.Request["Sex"];
                    user.Phone = Context.Request["Phone"];
                    user.Birthday = Convert.ToDateTime(Context.Request["Birthday"]); //日期转化
                    user.Department = Context.Request["Department"];
                    user.Email = Context.Request["Email"];
                    user.Institute = Context.Request["Institute"];

                    Manager.UpdateUser(user);
                    IsSuccess = true;

                    CurrentUser = user;

                    ResponseText = "{success:true,msg:'操作成功'}";
                }
                
            }
            catch (Exception e)
            {
                Message = e.Message;
            }

    }


    public void GetUser()
    {
        if (Validate(ERole.Student))
        {
            try
            {
                User user = CurrentUser;
                UserManager Manager = new UserManager();
                user = Manager.GetUser(CurrentUser.ID);
                IList<User> UserList = new List<User>();
                UserList.Add(user);
                string jsonWorks = JsonHelper.Serialize(UserList);
                IsSuccess = true;
                ResponseText = "{success:true," + string.Format("data:{0}", jsonWorks) + "}";
            }
            catch (Exception e)
            {
                Message = e.Message;    
            }
        }
        else if(Validate(ERole.Teacher))
        {
            try
            {
                User user = CurrentUser;
                UserManager Manager = new UserManager();
                user = Manager.GetUser(CurrentUser.ID);

                IList<User> UserList = new List<User>();
                UserList.Add(user);
                string jsonWorks = JsonHelper.Serialize(UserList);
                IsSuccess = true;
                ResponseText = "{success:true," + string.Format("data:{0}", jsonWorks) + "}";
            }
            catch (Exception e)
            {
                Message = e.Message;    
            }
        }
    }

    public void GetUserById()
    {
        if (Validate(ERole.Student) || Validate(ERole.Teacher))
        {
            try
            {
                string userId = Context.Request["stuID"];
                UserManager Manager = new UserManager();
                User user = Manager.GetUser(userId);
                IList<User> UserList = new List<User>();
                UserList.Add(user);
                string jsonWorks = JsonHelper.Serialize(UserList);
                IsSuccess = true;
                ResponseText = "{success:true," + string.Format("data:{0}", jsonWorks) + "}";
            }
            catch (Exception e)
            {
                Message = e.Message;
            }
        }
    }
}